package com.rainng.coursesystem.service;

import com.rainng.coursesystem.dto.FollowedUserDTO;
import com.rainng.coursesystem.model.FavouriteItem;
import com.rainng.coursesystem.model.FollowItem;
import com.rainng.coursesystem.model.StudentItem;
import com.rainng.coursesystem.model.TeacherItem;
import com.rainng.coursesystem.model.entity.TeacherEntity;
import com.rainng.coursesystem.repository.FavouriteRepository;
import com.rainng.coursesystem.repository.FollowRepository;
import com.rainng.coursesystem.repository.StudentFollowRepository;
import com.rainng.coursesystem.repository.TeacherFollowRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FollowService {

    @Autowired
    private FollowRepository followRepository;
    @Autowired
    private FavouriteRepository favouriteRepository;
    @Autowired
    private StudentFollowRepository studentFollowRepository;
    @Autowired
    private TeacherFollowRepository teacherFollowRepository;

    public void follow(String followerNumber, String followedNumber) throws Exception {
        if (followerNumber.equals(followedNumber)) {
            throw new Exception("不能关注自己");
        }
        if (followRepository.existsByFollowerNumberAndFollowedNumber(followerNumber, followedNumber)) {
            throw new Exception("已关注该用户");
        }

        FollowItem follow = new FollowItem(followerNumber, followedNumber);
        followRepository.save(follow);
    }

    public void unfollow(String followerNumber, String followedNumber) throws Exception {
        if (!followRepository.existsByFollowerNumberAndFollowedNumber(followerNumber, followedNumber)) {
            throw new Exception("未关注该用户");
        }
        followRepository.deleteByFollowerNumberAndFollowedNumber(followerNumber, followedNumber);
    }

//    public List<String> listFollowing(String followerNumber) {
//        return followRepository.findByFollowerNumber(followerNumber).stream()
//                .map(FollowItem::getFollowedNumber)
//                .collect(Collectors.toList());
//    }

    public List<FavouriteItem> listFollowedFavourites(String followerNumber) {
        List<String> followedNumbers = followRepository.findByFollowerNumber(followerNumber).stream()
                .map(FollowItem::getFollowedNumber)
                .collect(Collectors.toList());

        // 使用 IN 查询优化性能
        if (followedNumbers.isEmpty()) { //处理空列表情况
            return Collections.emptyList();//返回一个空列表
        }
        return favouriteRepository.findBySNumberInOrTNumberIn(followedNumbers,followedNumbers);
    }

    public List<FollowedUserDTO> listFollowing(String followerNumber) {
        List<String> followedNumbers = followRepository.findFollowedNumbersByFollowerNumber(followerNumber);
        List<FollowedUserDTO> result = new ArrayList<>();

        for (String number : followedNumbers) {
            String name = findNameByNumber(number);
            result.add(new FollowedUserDTO(number, name));
        }

        return result;
    }

    private String findNameByNumber(String number) {
        StudentItem student = studentFollowRepository.findByNumber(number).orElse(null);
        if (student != null) {
            return student.getName();
        }
        TeacherItem teacher = teacherFollowRepository.findByNumber(number).orElse(null);;
        if (teacher != null) {
            return teacher.getName();
        }
        return "Unknown"; // 或者可以返回 null，取决于你如何处理找不到用户的情况
    }

}